<?php


namespace App\Http\Controllers\Admin;
use App\Http\Controllers\BaseController;
use App\Models\Balance;
use App\Models\CookbookAdvView;
use App\Models\UnionOrder;
use App\Models\User;
use App\Services\BalanceService;
use Illuminate\Http\Request;

class CookbookAdvViewController extends BaseController
{

    public $model = CookbookAdvView::class;
    /**
     * 列表
     * @param Request $request
     * @return mixed
     */
    public function daren_index(Request $request) {
        $input = $request->input();
        $search = ['start_at' => '', 'end_at' => '', 'merchant_id' => ''];
        $benefit_user = $input['benefit_user'] ?? User::BENEFIT_DAREN;
        $list = CookbookAdvView::apply($search, $input)->where('merchant_id', '!=', 0)
            ->when($benefit_user, function ($q) use($benefit_user) {
                $q->where('benefit_user', $benefit_user);
            })
            ->groupBy('merchant_id')->selectRaw('merchant_id, count(*) as total')->orderBy('total', 'desc')->paginate(per_page());
        foreach ($list as $key => &$item) {
            $item->id = $key + 1;
            $item->finish_total = CookbookAdvView::apply($search, $input)->where('merchant_id', '=', $item->merchant_id)->where('is_finish', 1)->count();
        }
        return $this->paginate($list);
    }


    public function click_index(Request $request) {
        $search = ['start_at' => '', 'end_at' => ''];
        $input = $request->input();
        $is_finish = $request->input('is_finish', -1);
        $nickname = $request->input('nickname');
        $benefit_user = $input['benefit_user'] ?? User::BENEFIT_DAREN;
        $list = CookbookAdvView::apply($search, $input)->when($is_finish != -1, function ($q) use($is_finish){
            $q->where('is_finish', $is_finish);
        })->when($benefit_user, function ($q) use($benefit_user) {
            $q->where('benefit_user', $benefit_user);
        })->when($nickname, function ($q) use($nickname){
            $q->whereHas('user', function ($qt) use($nickname){
                $qt->where('nickname', 'like', '%' . $nickname . '%');
            });
        })->withCertain('user', ['nickname'])->orderBy('id', 'desc')->paginate(per_page());
        return $this->paginate($list);
    }


    public function budget_index(Request $request) {
        $input = $request->input();
        $search = ['merchant_id' => ''];
        $benefit_user = $input['benefit_user'] ?? User::BENEFIT_DAREN;
        $month = $request->input('month');
        if ($month) {
            $month = date('Ym', strtotime($month));
        } else {
            $month = date('Ym');
        }
        $list = CookbookAdvView::apply($search, $input)->where('merchant_id', '!=', 0)
            ->when($benefit_user, function ($q) use($benefit_user) {
                $q->where('benefit_user', $benefit_user);
            })
            ->when($month, function ($q) use($month){
                $q->where('month', $month);
            })
            ->groupBy('merchant_id')->selectRaw('merchant_id, count(*) as total')->orderBy('total', 'desc')->paginate(per_page());
        foreach ($list as $key => &$item) {
            $item->id = $key + 1;
            $item->finish_total = CookbookAdvView::apply($search, $input)->where('merchant_id', '=', $item->merchant_id)->where('is_finish', 1)->count();
            $amount = Balance::where('extra', $month)->where('merchant_id', $item->merchant_id)->value('amount');
            $item->amount = $amount ? format_yuan($amount) : null;
        }
        return $this->paginate($list);
    }

    public function store_budget(Request $request) {
        $input = $request->input();
        $this->validate($request, [
            'month' => 'required',
            'unit_price' => 'required',
            'merchant_id' => 'required',
        ]);

        $month = date('Ym', strtotime($input['month']));
        if ($month >= date('Ym'))
            return $this->failed('该月份还未结束，暂不能结算');

        $merchant_id = $input['merchant_id'];
        //判断是否结算过
        $exists = Balance::where('extra', $month)->where('merchant_id', $merchant_id)->exists();
        if ($exists)
            return $this->failed('已经结算过');

        $total_finish = CookbookAdvView::where('merchant_id', $merchant_id)->where('month', $month)->where('is_finish', 1)->count();

        $total_amount = $total_finish * $input['unit_price'] * 100;
        $title = '激励视频';
        $benefit_user = CookbookAdvView::where('merchant_id', $merchant_id)->value('benefit_user');
        BalanceService::store($benefit_user, $merchant_id, $total_amount, Balance::TYPE_COOKBOOK_VIDEO, 0, '', $title, true, 0, $month);

        //流水
        UnionOrder::create([
            'user_id' => 0,
            'order_id' => 0,
            'type' => UnionOrder::TYPE_COOKBOOK_VIDEO,
            'order_sn' => '',
            'amount' => $total_amount,
            'merchant_id' => $merchant_id,
            'created_at' => date('Y-m-d')
        ]);

        return $this->success(['total_amount' => $total_finish * $input['unit_price']]);
    }

}

